#pragma once

#include <mythread.hxx>
#include <string>
#include <map>
#include <vector>
#include "auto.hxx"

class Task;
class TaskManager;
class FileLog;

class SrvPerfWatcher : public Thread
{
public:
	SrvPerfWatcher(TaskManager* taskManager, DMFun f, int watchInterval, FileLog* filelog = NULL);

	void Shutdown(void);
	int GetLatestDuration(const string& port) const;
	const string GetGoodPort(void) const;
	std::string GetSrvPerfInfo(void) const;

protected:
	
	DMFun DisplayMsg;

	virtual void Execute(Thread::Arg);
	void ForceSwitch(std::vector<std::string> slowPorts);

private:
	///////////////////////////////////////////////////
	//////////// inner tester////////////////////////////
	class Tester : public Thread
	{
	public:
		//////////////////////////////////////
		/////////// ctors
		Tester(DMFun DisplayMsg1, FileLog* filelog);

		void Shutdown(void);
	protected:
		virtual void Execute(Thread::Arg);

		DMFun DisplayMsg;

		FileLog* _filelog;
	};

	typedef struct
	{
		string port;
		int duration;
		int lastDuration;
		Task* seed;
		Tester* tester;
	}TestingUnit;

	TaskManager* taskManager;
	mutable std::map<std::string, TestingUnit*> PortPerfMap;
	string goodPort;
	int WatchInterval;
	bool isShutdownRequested;
	FileLog* _filelog;
};
